package com.jy.adscm.operation.base.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.JySoftDescription;
import com.jy.adscm.operation.base.service.JySoftDescriptionService;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingAnnex;
import com.jy.adscm.zltx.bean.JyDrawingEditInfo;
import com.jy.adscm.zltx.bean.JyDrawingList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-01-10
 */
@RestController
@RequestMapping("/adscm/operation/jySoftDescription")
public class JySoftDescriptionController {
     @Autowired
    JySoftDescriptionService jySoftDescriptionService;
    @Value("${jySoftDes.upload-path}")
    private  String jySoftDesUploadPath;
    @RequestMapping("/upload/{mid}")
    public SimpleCoreResponse update(@PathVariable Integer mid,HttpServletRequest request) throws AdscmSystemException {

        List<String> paths = null;

        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());

        if(multipartResolver.isMultipart(request)){
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

            String path = jySoftDesUploadPath+ File.separator + UUID.randomUUID().toString().replaceAll("-","") + File.separator;

            paths = AdscmSystemUtils.getInstance().uploadFileNoNewName(multiRequest,path);
        }
        if (paths != null){
            for (String path : paths) {
                //删除原有的文件
                JySoftDescription jySoftDescription = jySoftDescriptionService.selectById(mid);
                if(AdscmBaseUtils.getInstance().isNotEmpty(jySoftDescription,null)){
                    File  file = new File(jySoftDescription.getPath());
                    if ( file.exists()){
                        file.delete();
                    }
                    jySoftDescription.setMid(mid);
                    jySoftDescription.setPath(path);
                    jySoftDescriptionService.updateById(jySoftDescription);
                }else {
                    JySoftDescription jySoftDescription1 =new JySoftDescription();
                    jySoftDescription1.setMid(mid);
                    jySoftDescription1.setPath(path);
                    jySoftDescriptionService.insert(jySoftDescription1);
                }



            }
        }
        return new SimpleCoreResponse("200","上传成功" ,paths);
    }
    @RequestMapping("/view/{mid}")
    public void viewPdf(@PathVariable Integer mid, HttpServletResponse response) throws AdscmSystemException {
        String  filePath=null;
        JySoftDescription jySoftDescription = jySoftDescriptionService.selectById(mid);
          if (AdscmBaseUtils.getInstance().isNotEmpty(jySoftDescription.getPath(),null)){
              filePath=jySoftDescription.getPath();
          }else {
              throw  new AdscmSystemException("路径不存在");
          }
        AdscmSystemUtils.getInstance().writeFile(filePath,response);
    }

    @RequestMapping("/downloadPdf/{mid}")
    public void downloadPdf(@PathVariable Integer mid, HttpServletResponse response) throws AdscmSystemException {

        String filePath = null;
        JySoftDescription jySoftDescription = jySoftDescriptionService.selectById(mid);
        if (AdscmBaseUtils.getInstance().isNotEmpty(jySoftDescription.getPath(),null)){
            filePath=jySoftDescription.getPath();
        }else {
            throw  new AdscmSystemException("路径不存在");
        }
        AdscmSystemUtils.getInstance().downloadFile(filePath,response);
    }


}

